const router = require('koa-router')()
const {getSchool, postSchool, patchSchool, deleteSchool, getSchoolById} = require('../../controller/school')

router.prefix('/api/school')

router.get('/school/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getSchoolById(id)
})

router.get('/school', async (ctx, next) => {
  const {name, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  const area_id = ctx.query['area_id[]']
  ctx.body = await getSchool({name, pageIndex, pageSize, createTime, area_id})
})

router.post('/school', async (ctx, next) => {
  const {name, areaId} = ctx.request.body
  ctx.body = await postSchool({name, areaId})
})

router.patch('/school', async (ctx, next) => {
  const {name, areaId, id} = ctx.request.body
  ctx.body = await patchSchool({name, areaId, id})
})

router.delete('/school/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await deleteSchool(id)
})

module.exports = router
